﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using XfTech.SmartTms.Entities;

namespace XfTech.SmartTms.Mappings
{
    /// <summary>
    /// 车辆实体类映射文件（FluentApi来配置映射规则）
    /// </summary>
    public class VehicleMap : IEntityTypeConfiguration<Vehicle>
    {
        public void Configure(EntityTypeBuilder<Vehicle> builder)
        {
            builder.ToTable("Vehicles");

            builder.Property("Brand").HasMaxLength(40).IsRequired();
            builder.Property("LicensePlate").HasMaxLength(15).IsRequired();
            builder.Property("Driver").HasMaxLength(10).IsRequired();
            builder.Property("Company").HasMaxLength(30).IsRequired();
            builder.Property("Color").HasMaxLength(10).IsRequired();
            builder.Property("VehicleNo").HasMaxLength(30).IsRequired();

            builder.Property("BuyDate").HasColumnType("date").IsRequired();
            builder.Property("InsuranceDate").HasColumnType("date").IsRequired();
            builder.Property("AnnualCheckDate").HasColumnType("date").IsRequired();

            builder.Property("VehiclePhoto").HasMaxLength(255).IsRequired();
            builder.Property("InsurancePhoto").HasMaxLength(255).IsRequired();

            //配置值对象VehicleSize的映射规则
            builder.OwnsOne(
                o => o.Size,
                s=>
                {
                    s.Property(s => s.Length).HasColumnName("VehivleLength");
                    s.Property(s => s.Width).HasColumnName("VehivleWidth");
                    s.Property(s => s.Height).HasColumnName("VehivleHeight");
                }
            );
        }
    }
}
